package com.stylefeng.guns.modular.verbal.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.system.dao.QaMapper;
import com.stylefeng.guns.modular.system.model.Qa;
import com.stylefeng.guns.modular.verbal.service.IQaService;
import com.stylefeng.guns.modular.verbal.warpper.QaWarpper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 问题控制器
 *
 * @author fengshuonan
 * @Date 2018-04-10 13:06:34
 */
@Controller
@RequestMapping("/qa")
public class QaController extends BaseController {

    private String PREFIX = "/verbal/qa/";

    @Autowired
    private IQaService qaService;
    @Autowired
    private QaMapper qaMapper;

    /**
     * 跳转到问题首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "qa.html";
    }

    /**
     * 跳转到添加问题
     */
    @RequestMapping("/qa_add")
    public String qaAdd() {
        return PREFIX + "qa_add.html";
    }

    /**
     * 跳转到修改问题
     */
    @RequestMapping("/qa_update/{qaId}")
    public String qaUpdate(@PathVariable Integer qaId, Model model) {
        Qa qa = qaService.selectById(qaId);
        Map<String, Object> map = BeanKit.beanToMap(qa);
        model.addAttribute("item", new QaWarpper(map).warp());
        LogObjectHolder.me().set(qa);
        return PREFIX + "qa_edit.html";
    }

    /**
     * 获取问题列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "typeCode", required = false) String typeCode
            , @RequestParam(value = "question", required = false) String question) {
        Page<Qa> page = new PageFactory<Qa>().defaultPage();
        List<Map<String, Object>> list = qaMapper.selectQaList(page,question, typeCode);
        page.setRecords((List<Qa>) new QaWarpper(list).warp());
        return super.packForBT(page);
    }

    /**
     * 新增问题
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @ApiOperation(value = "新增问题", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "question", value = "问题", dataType = "string", required = true),
                    @ApiImplicitParam(name = "answer", value = "答案", dataType = "string", required = true),
                    @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true)
            }
    )
    public Object add( @Validated Qa qa) {
        qa.setCreateTime(new Date());
        qaService.insert(qa);
        return SUCCESS_TIP;
    }


    /**
     * 删除问题
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer qaId) {
        qaService.deleteById(qaId);
        return SUCCESS_TIP;
    }

    /**
     * 修改问题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @ApiOperation(value = "更新问题", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "id", value = "id", dataType = "integer", required = true),
                    @ApiImplicitParam(name = "question", value = "问题", dataType = "string", required = true),
                    @ApiImplicitParam(name = "answer", value = "答案", dataType = "string", required = true),
                    @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true)
            }
    )
    public Object update(@Valid Qa qa) {
        qaService.updateById(qa);
        return SUCCESS_TIP;
    }

    /**
     * 问题详情
     */
    @RequestMapping(value = "/detail/{qaId}")
    @ResponseBody
    public Object detail(@PathVariable("qaId") Integer qaId) {
        return qaService.selectById(qaId);
    }
}
